{"ast":null,"code":"import { makeAutoObservable } from \"mobx\";\nimport { UserService, AuthService, ChatService } from \"../services/\";\nimport axios from \"axios\";\nimport { refreshToken } from \"../routes\";\nexport default class Store {\n  constructor() {\n    this.isAuth = false;\n    this.profile = {};\n    this.chats = {};\n    this.isLoading = false;\n    this.isError = false;\n    this.getAuth = () => {\n      return this.isAuth;\n    };\n    this.getLoading = () => {\n      return this.isLoading;\n    };\n    makeAutoObservable(this);\n  }\n  setAuth(bool) {\n    this.isAuth = bool;\n  }\n  getProfile() {\n    return this.profile;\n  }\n  setProfile(profile) {\n    this.profile = profile;\n  }\n  getChats() {\n    return this.chats;\n  }\n  setChats(chats) {\n    this.chats = chats;\n  }\n  setLoading(bool) {\n    this.isLoading = bool;\n  }\n  setIsError(bool) {\n    this.isError = bool;\n  }\n  getIsError() {\n    return this.isError;\n  }\n  setTokensTolLocalStorage(response) {\n    localStorage.setItem(`${process.env.REACT_APP_NAME_TOKEN}`, response.data.token);\n    localStorage.setItem(`${process.env.REACT_APP_NAME_REFRESH_TOKEN}`, response.data.refresh_token);\n  }\n  removeTokensTolLocalStorage() {\n    localStorage.removeItem(`${process.env.REACT_APP_NAME_TOKEN}`);\n    localStorage.removeItem(`${process.env.REACT_APP_NAME_REFRESH_TOKEN}`);\n  }\n  async getProfileFromAPI() {\n    try {\n      const response = await UserService.profile();\n      this.setProfile(response.data);\n    } catch (e) {\n      this.removeTokensTolLocalStorage();\n      this.setIsError(true);\n    }\n  }\n  async signIn(request) {\n    try {\n      const response = await AuthService.signIn(request);\n      this.setTokensTolLocalStorage(response);\n      this.setAuth(true);\n      await this.getProfileFromAPI();\n    } catch (e) {\n      this.setIsError(true);\n    }\n  }\n  async signUp(request) {\n    try {\n      const response = await AuthService.signUp(request);\n      this.setTokensTolLocalStorage(response);\n      this.setAuth(true);\n      await this.getProfileFromAPI();\n    } catch (e) {\n      this.setIsError(true);\n    }\n  }\n  async forgotPassword(request) {\n    try {\n      await AuthService.forgotPassword(request);\n    } catch (e) {\n      this.setIsError(true);\n    }\n  }\n  logout() {\n    this.removeTokensTolLocalStorage();\n    this.setAuth(false);\n    this.setProfile({});\n  }\n  async checkAuth() {\n    this.setLoading(true);\n    try {\n      const response = await axios.post(refreshToken(), {\n        \"refresh_token\": localStorage.getItem(`${process.env.REACT_APP_NAME_REFRESH_TOKEN}`)\n      });\n      this.setTokensTolLocalStorage(response);\n      this.setAuth(true);\n      await this.getProfileFromAPI();\n    } catch (e) {\n      this.removeTokensTolLocalStorage();\n      this.setIsError(true);\n    } finally {\n      this.setLoading(false);\n    }\n  }\n  async getChatsFromAPI() {\n    try {\n      return await ChatService.getChatsList().then(res => console.log(JSON.parse(JSON.stringify(res.data)).items[0].lastMessage.created_at)\n      //this.setChats(res.data)\n      );\n    } catch (e) {\n      console.error(e.response.data.message);\n    }\n  }\n}","map":{"version":3,"names":["makeAutoObservable","UserService","AuthService","ChatService","axios","refreshToken","Store","constructor","isAuth","profile","chats","isLoading","isError","getAuth","getLoading","setAuth","bool","getProfile","setProfile","getChats","setChats","setLoading","setIsError","getIsError","setTokensTolLocalStorage","response","localStorage","setItem","process","env","REACT_APP_NAME_TOKEN","data","token","REACT_APP_NAME_REFRESH_TOKEN","refresh_token","removeTokensTolLocalStorage","removeItem","getProfileFromAPI","e","signIn","request","signUp","forgotPassword","logout","checkAuth","post","getItem","getChatsFromAPI","getChatsList","then","res","console","log","JSON","parse","stringify","items","lastMessage","created_at","error","message"],"sources":["/data/messenger.client/src/store/store.ts"],"sourcesContent":["import {makeAutoObservable} from \"mobx\";\nimport SignInRequest from \"../models/request/SignInRequest\";\nimport SignUpRequest from \"../models/request/SignUpRequest\";\nimport {UserService, AuthService, ChatService} from \"../services/\";\nimport {UserListItem} from \"../models/response/UserListItem\";\nimport axios, {AxiosResponse} from \"axios\";\nimport {AuthResponse} from \"../models/response/AuthResponse\";\nimport {refreshToken} from \"../routes\";\nimport ForgotPasswordRequest from \"../models/request/ForgotPasswordRequest\";\nimport {ChatsListResponse} from \"../models/response/ChatsListResponse\";\n\nexport default class Store {\n\n    private isAuth: boolean = false;\n    private profile = {} as UserListItem;\n    public chats = {} as ChatsListResponse;\n    private isLoading: boolean = false;\n    private isError: boolean = false;\n\n    public constructor() {\n        makeAutoObservable(this);\n    }\n\n    private setAuth(bool: boolean): void {\n        this.isAuth = bool;\n    }\n\n    public getAuth: () => boolean = () => {\n        return this.isAuth;\n    }\n\n    public getProfile(): UserListItem {\n        return this.profile;\n    }\n\n    private setProfile(profile: UserListItem): void {\n        this.profile = profile;\n    }\n\n    public getChats(): ChatsListResponse {\n        return this.chats;\n    }\n\n    private setChats(chats: ChatsListResponse): void {\n        this.chats = chats;\n    }\n\n    private setLoading(bool: boolean): void {\n        this.isLoading = bool;\n    }\n\n    public getLoading: () => boolean = () => {\n        return this.isLoading;\n    }\n\n    public setIsError(bool: boolean): void {\n        this.isError = bool;\n    }\n\n    public getIsError(): boolean {\n        return this.isError;\n    }\n\n    public setTokensTolLocalStorage(response: AxiosResponse<AuthResponse>): void {\n        localStorage.setItem(\n            `${process.env.REACT_APP_NAME_TOKEN}`,\n            response.data.token\n        );\n        localStorage.setItem(\n            `${process.env.REACT_APP_NAME_REFRESH_TOKEN}`,\n            response.data.refresh_token\n        );\n    }\n\n    public removeTokensTolLocalStorage(): void {\n        localStorage.removeItem(`${process.env.REACT_APP_NAME_TOKEN}`);\n        localStorage.removeItem(`${process.env.REACT_APP_NAME_REFRESH_TOKEN}`);\n    }\n\n    private async getProfileFromAPI() {\n        try {\n            const response = await UserService.profile();\n            this.setProfile(response.data);\n        } catch (e) {\n            this.removeTokensTolLocalStorage();\n            this.setIsError(true);\n        }\n    }\n\n    public async signIn(request: SignInRequest) {\n        try {\n            const response = await AuthService.signIn(request);\n            this.setTokensTolLocalStorage(response);\n            this.setAuth(true);\n            await this.getProfileFromAPI();\n        } catch (e) {\n            this.setIsError(true);\n        }\n    }\n\n    public async signUp(request: SignUpRequest) {\n        try {\n            const response = await AuthService.signUp(request);\n            this.setTokensTolLocalStorage(response);\n            this.setAuth(true);\n            await this.getProfileFromAPI();\n        } catch (e) {\n            this.setIsError(true);\n        }\n    }\n\n    public async forgotPassword(request: ForgotPasswordRequest) {\n        try {\n            await AuthService.forgotPassword(request);\n        } catch (e) {\n            this.setIsError(true);\n        }\n    }\n\n    public logout() {\n        this.removeTokensTolLocalStorage();\n        this.setAuth(false);\n        this.setProfile({} as UserListItem);\n    }\n\n    public async checkAuth() {\n        this.setLoading(true);\n        try {\n            const response = await axios.post<AuthResponse>(\n                refreshToken(),\n                {\n                    \"refresh_token\": localStorage.getItem(`${process.env.REACT_APP_NAME_REFRESH_TOKEN}`)\n                }\n            );\n            this.setTokensTolLocalStorage(response);\n            this.setAuth(true);\n            await this.getProfileFromAPI();\n        } catch (e) {\n            this.removeTokensTolLocalStorage();\n            this.setIsError(true);\n        } finally {\n            this.setLoading(false);\n        }\n    }\n\n    public async getChatsFromAPI() {\n        try {\n            return await ChatService.getChatsList().then((res: any) => (\n                console.log(JSON.parse(JSON.stringify(res.data)).items[0].lastMessage.created_at)\n                //this.setChats(res.data)\n            ));\n        } catch (e: any) {\n            console.error(e.response.data.message);\n        }\n    }\n}"],"mappings":"AAAA,SAAQA,kBAAkB,QAAO,MAAM;AAGvC,SAAQC,WAAW,EAAEC,WAAW,EAAEC,WAAW,QAAO,cAAc;AAElE,OAAOC,KAAK,MAAuB,OAAO;AAE1C,SAAQC,YAAY,QAAO,WAAW;AAItC,eAAe,MAAMC,KAAK,CAAC;EAQhBC,WAAW,GAAG;IAAA,KANbC,MAAM,GAAY,KAAK;IAAA,KACvBC,OAAO,GAAG,CAAC,CAAC;IAAA,KACbC,KAAK,GAAG,CAAC,CAAC;IAAA,KACTC,SAAS,GAAY,KAAK;IAAA,KAC1BC,OAAO,GAAY,KAAK;IAAA,KAUzBC,OAAO,GAAkB,MAAM;MAClC,OAAO,IAAI,CAACL,MAAM;IACtB,CAAC;IAAA,KAsBMM,UAAU,GAAkB,MAAM;MACrC,OAAO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